package com.huixin.zhang.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AmountUtil {

	public static long parseAmountStr2Long(String amountStr) {
		if ((amountStr == null) || ("".equals(amountStr))) {
			return 0L;
		}
		double amount = Double.parseDouble(amountStr);
		Double db = Double.valueOf(amount * 100.0D);
		DecimalFormat df = new DecimalFormat("#");
		String s = df.format(db);
		return Long.parseLong(s);
	}

	public static String parseAmountLong2Str(Long amountLong) {
		if (amountLong == null) {
			return "0.00";
		}

		DecimalFormat df = new DecimalFormat("#,###.##");
		double d = amountLong.longValue() / 100.0D;
		String s = df.format(d);
		return s;
	}

	public static String format2Decimal(BigDecimal amount) {
		if (amount == null) {
			return "0.00";
		}
		DecimalFormat df = new DecimalFormat("#,###.##");
		String s = df.format(amount.movePointLeft(2));
		return s;
	}

	public static String format2Decimal(Long amount) {
		if (amount == null) {
			return "0.00";
		}
		DecimalFormat df = new DecimalFormat("#,###.##");
		String s = df.format(amount/100.0);
		return s;
	}

	public static  String formatPercent(BigDecimal amount){
		if (amount == null) {
			return "0.00%";
		}
		return new DecimalFormat("0.00%").format(amount);
	}

	public static Long yuan2Fen(Double yuan) {
		Double dFen = Double.valueOf(yuan.doubleValue() * 100.0D);
		Long lFen = Long.valueOf(dFen.longValue());
		return lFen;
	}

	public static Double fen2Yuan(Long fen) {
		Double yuan = Double.valueOf(fen.longValue() / 100.0D);
		BigDecimal big = new BigDecimal(fen.longValue() / 100.0D);
		yuan = Double.valueOf(big.setScale(2, 4).doubleValue());
		return yuan;
	}

	public static String fen2YuanNew(Long fen) {
		if (fen == null) {
			return "0";
		}
		String yuan = BigDecimal.valueOf(fen.longValue()).divide(new BigDecimal(100)).setScale(2, 4).toString();

		return yuan;
	}

	public static String fen2WanYuanNew(Long fen) {
		if (fen == null) {
			return "0";
		}
		String yuan = BigDecimal.valueOf(fen.longValue()).divide(new BigDecimal(1000000)).setScale(2, 4).toString();

		return yuan;
	}

	public static double div(double v1, double v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}

		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.divide(b2, scale, 4).doubleValue();
	}

	public static String spitStr(String amountStr) {
		int index = amountStr.indexOf(".");
		if (index != -1) {
			amountStr = amountStr.substring(0, index);
		}
		return amountStr;
	}

	public static double div(BigDecimal v1, BigDecimal v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}

		return v1.divide(v2, scale, 4).doubleValue();
	}

	public static Double to2dec(Number number, int digit) {
		if (number != null) {
			NumberFormat format = NumberFormat.getInstance();
			format.setMaximumFractionDigits(digit);
			format.setGroupingUsed(false);
			return Double.valueOf(format.format(number.doubleValue()));
		}
		return Double.valueOf(0.0D);
	}

	public static double div2(BigDecimal v1, double v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}

		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return v1.divide(b2, scale, 4).doubleValue();
	}

	public static long nullT0(Long params) {
		if(params == null)
			return 0;
		else
			return params;
	}

	public static Date getDateFormatyyyyMMddHHmmss(String dateStr) {
		try {
			Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse(dateStr);
			return date;
		} catch (Exception e) {
			return new Date();
		}
	}

	public static void main(String[] args) {
		System.out.println(getDateFormatyyyyMMddHHmmss("20131101102030"));
	}
}